<template>
  <!--股东信息  -->
  <div>
    <FormPanel
      ref="FormPanel"
      :list="initForm"
      :cols="2"
      :formType="formType"
      :labelWidth="150"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
      class="mtop"
    />
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import * as types from '@/api/cms-api/origin/originInfo/types';

import * as utils from '@/utils/utils'; //工具类

import vm from './bridge';

export default {
  mixins: [dictionary],
  model: {
    prop: 'pdata',
    event: 'click'
  },
  props: {
    pdata: {
      type: Object,
      default: () => ({})
    },
    formType: {
      type: String,
      required: true
    },
    formOptions: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      initForm: this.initFormFun()
    };
  },
  mounted() {
    //注册经销商类型变化时触发的事件
    //在审核时，给此页面的类型为只读，但是基本信息（infoBase可以编辑，会显示会触发事件，事件会触发禁用取消，覆盖整个表单的类型（表单类型为show，但是依旧可以输入，（部分）））
    if (this.formType != 'show') {
      vm.$on('changeDealerType', this.changeDealerType);
    }
  },
  methods: {
    //初始化数据
    init() {
      this.initForm.forEach(x => {
        if (this.pdata[x.fieldName]) {
          x.initialValue = this.pdata[x.fieldName];
        }
      });
    },
    formChangeHandle(val) {
      this.pdata = Object.assign(this.pdata, val);
      //传递数据到父组件里
      this.$emit('click', this.pdata);
      //触发外部的事件，以标志着本表单已经准备完毕
      this.$emit('formchange', this.pdata);
    },
    saveDialog() {
      if (this.verificationEquityRatio()) {
        this.$refs.FormPanel.SUBMIT_FORM();
      } else {
        try {
          //(滚动到xx模块)需要父页面的this
          //this.$parent.$el.children[0].children[4].click();
        } catch (e) {
          // console.error("this.$el.children[0].children[4],出现错误，请检查",e,this.$el);
        }
        this.$notify.warning({ title: '提示信息', message: '公司信息占比总和不等于100，请核对!' });
      }
    },
    //验证‘资金占比’是否为100
    verificationEquityRatio() {
      let equityRatio1 = this.$refs.FormPanel.form.equityRatio1;
      let equityRatio2 = this.$refs.FormPanel.form.equityRatio2;
      let equityRatio3 = this.$refs.FormPanel.form.equityRatio3;
      let equityRatio = this.$refs.FormPanel.form.equityRatio;
      if (!equityRatio1 && !equityRatio2 && !equityRatio3 && !equityRatio) {
        return true;
      }
      //
      this.$refs.FormPanel.excuteFormData(this.$refs.FormPanel.form);
      equityRatio1 = parseFloat(equityRatio1);
      equityRatio2 = parseFloat(equityRatio2);
      equityRatio3 = parseFloat(equityRatio3);
      equityRatio = parseFloat(equityRatio);

      //如果存在NaN或Infinity则置为0
      if (isNaN(equityRatio1) || equityRatio1 === Infinity) {
        equityRatio1 = 0;
      }
      if (isNaN(equityRatio2) || equityRatio2 === Infinity) {
        equityRatio2 = 0;
      }
      if (isNaN(equityRatio3) || equityRatio3 === Infinity) {
        equityRatio3 = 0;
      }
      if (isNaN(equityRatio) || equityRatio === Infinity) {
        equityRatio = 0;
      }

      if (equityRatio1 + equityRatio2 + equityRatio3 + equityRatio === 100) {
        return true;
      }

      return false;
    },
    //经销商类型变更时
    changeDealerType(value) {
      console.log('this.formType =>', this.formType);
      //判断是否是dle页面，是则使用dlr专用验证
      let suffix = this.formOptions.formType === types.DEALER_INFO_FORM_STATUS_SUBMISSION_DLR ? 'DLR' : '';
      //根据类型处理验证
      if (types.DEALER_INFO_DEALER_TYPE_4S === value) {
        //4S
        vm.changeForm(this.initForm, '4S' + suffix, false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_SINGLE_AFTER_SALE === value) {
        //单一售后
        vm.changeForm(this.initForm, 'SH' + suffix, false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_SINGLE_SALE === value) {
        //单一销售
        vm.changeForm(this.initForm, 'XS' + suffix, false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_DIRECT_SHOP === value) {
        //SPACE
        vm.changeForm(this.initForm, 'SPACE' + suffix, false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_NO_DIRECT_SHOP === value) {
        //RACE
        vm.changeForm(this.initForm, 'RACE' + suffix, false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_BIG_CUSTOMER === value) {
        //大客户
        vm.changeForm(this.initForm, 'DKH' + suffix, false);
        vm.clearValidate(this.$refs.FormPanel);
      }
    },
    //搜索数据
    initFormFun() {
      let bt = { required: true, message: '必填', trigger: ['change', 'blur'] };
      let btl = [];
      let sz = { pattern: /^[1-9]\d*$/, message: '请填写正确的数字', trigger: 'blur' };
      return [
        {
          type: 'INPUT',
          label: '股东一',
          fieldName: 'shareholderName1',
          initialValue: '',
          maxlength: 100,
          rules: btl,
          rules4S: [], //4S店
          rulesSH: [], //单一售后
          rulesXS: [], //单一销售
          rulesSPACE: [], //SPACE
          rulesRACE: [], //RACE
          rulesDKH: [], //大客户t

          rules4SDLR: btl, //4S店(dlr)
          rulesSHDLR: btl, //单一售后(dlr)
          rulesXSDLR: btl, //单一销售(dlr)
          rulesSPACEDLR: btl, //SPACE(dlr)
          rulesRACEDLR: btl, //RACE(dlr)
          rulesDKHDLR: [] //大客户(dlr)t
        },
        {
          type: 'INPUT',
          label: '资金',
          fieldName: 'inputAmount1',
          initialValue: '',
          maxlength: 15,
          unitRender: () => {
            return <span>万</span>;
          },
          change: val => {
            utils.toNumber(this.initForm, 'inputAmount1', val);
          },
          rules: btl,
          rules4S: [],
          rulesSH: [],
          rulesXS: [],
          rulesSPACE: [],
          rulesRACE: [],
          rulesDKH: [], //t

          rules4SDLR: btl,
          rulesSHDLR: btl,
          rulesXSDLR: btl,
          rulesSPACEDLR: btl,
          rulesRACEDLR: btl,
          rulesDKHDLR: [] //t
        },
        {
          type: 'INPUT',
          label: '占比',
          fieldName: 'equityRatio1',
          initialValue: '',
          maxlength: 3,
          unitRender: () => {
            return <span>%</span>;
          },
          rules: [sz],
          rules4S: [sz],
          rulesSH: [sz],
          rulesXS: [sz],
          rulesSPACE: [sz],
          rulesRACE: [sz],
          rulesDKH: [sz], //t

          rules4SDLR: [sz],
          rulesSHDLR: [sz],
          rulesXSDLR: [sz],
          rulesSPACEDLR: [sz],
          rulesRACEDLR: [sz],
          rulesDKHDLR: [sz] //t
        },

        { type: 'INPUT', label: '股东二', fieldName: 'shareholderName2', initialValue: '', maxlength: 100 },
        {
          type: 'INPUT',
          label: '资金',
          fieldName: 'inputAmount2',
          initialValue: '',
          maxlength: 15,
          unitRender: () => {
            return <span>万</span>;
          },
          change: val => {
            utils.toNumber(this.initForm, 'inputAmount2', val);
          }
        },
        {
          type: 'INPUT',
          label: '占比',
          fieldName: 'equityRatio2',
          initialValue: '',
          maxlength: 3,
          unitRender: () => {
            return <span>%</span>;
          },
          rules: [sz],
          rules4S: [sz],
          rulesSH: [sz],
          rulesXS: [sz],
          rulesSPACE: [sz],
          rulesRACE: [sz],
          rulesDKH: [sz], //t

          rules4SDLR: [sz],
          rulesSHDLR: [sz],
          rulesXSDLR: [sz],
          rulesSPACEDLR: [sz],
          rulesRACEDLR: [sz],
          rulesDKHDLR: [sz] //t
        },

        { type: 'INPUT', label: '股东三', fieldName: 'shareholderName3', initialValue: '', maxlength: 100 },
        {
          type: 'INPUT',
          label: '资金',
          fieldName: 'inputAmount3',
          initialValue: '',
          maxlength: 15,
          unitRender: () => {
            return <span>万</span>;
          },
          change: val => {
            utils.toNumber(this.initForm, 'inputAmount3', val);
          }
        },
        {
          type: 'INPUT',
          label: '占比',
          fieldName: 'equityRatio3',
          initialValue: '',
          maxlength: 3,
          unitRender: () => {
            return <span>%</span>;
          },
          rules: [sz],
          rules4S: [sz],
          rulesSH: [sz],
          rulesXS: [sz],
          rulesSPACE: [sz],
          rulesRACE: [sz],
          rulesDKH: [sz], //t

          rules4SDLR: [sz],
          rulesSHDLR: [sz],
          rulesXSDLR: [sz],
          rulesSPACEDLR: [sz],
          rulesRACEDLR: [sz],
          rulesDKHDLR: [sz]
        },

        { type: 'INPUT', label: '其他', fieldName: 'shareholderName', initialValue: '', maxlength: 100 },
        {
          type: 'INPUT',
          label: '资金',
          fieldName: 'inputAmount',
          initialValue: '',
          maxlength: 15,
          unitRender: () => {
            return <span>万</span>;
          },
          change: val => {
            utils.toNumber(this.initForm, 'inputAmount', val);
          }
        },
        {
          type: 'INPUT',
          label: '占比',
          fieldName: 'equityRatio',
          initialValue: '',
          maxlength: 3,
          unitRender: () => {
            return <span>%</span>;
          },
          rules: [sz],
          rules4S: [sz],
          rulesSH: [sz],
          rulesXS: [sz],
          rulesSPACE: [sz],
          rulesRACE: [sz],
          rulesDKH: [sz],

          rules4SDLR: [sz],
          rulesSHDLR: [sz],
          rulesXSDLR: [sz],
          rulesSPACEDLR: [sz],
          rulesRACEDLR: [sz],
          rulesDKHDLR: [sz]
        }
      ];
    }
  }
};
</script>
